/*
 * @Author: yuqy
 * @Date: 2023-09-26 18:13:33
 * @LastEditors: yuqy
 * @LastEditTime: 2023-10-24 15:44:01
 * @Descripttion: -
 */
import { mapState, mapGetters, mapActions } from 'vuex'
import { showConfirmDialog } from "vant";
import { goUsercenter, openAudience } from "@/libs/homeredirect";
import util from "@/libs/util";
// import { Message } from 'view-ui-plus';
// 用于页面间通信
const userMsgBc = new BroadcastChannel('getUserMainMsg');
const myIndexedDB = util.getLocalforage();
const minix = {
    data() {
        return {
            code: null,
            isOpenWhiteboard: false, // 是否打开互动画板
            windoWaudience: null, // 副屏展示页面 对象
            isHasSecondScreen: false, // 是否打开了副屏 经过副屏发送信息验证
            isAllWrite: false, // 听讲是否可以进行书写
            isShowLiveWrap: false // 听讲老师是否选择了观看直播
        }
    },
    created() {
        this.code = this.$route.query.code;
        if (this.code) {
            this.getRoomD()
        }
    },
    unmounted () {
        console.log('我离开了哦');
    },
    async mounted() {
        this.getInitBC()
        this.sendUserListToAudience()
    },
    computed: {
        ...mapState({
            roomInfo: (state) => state.roomAgora.roomInfo,
            showLiveing: (state) => state.roomAgora.showLiveing,// 当前房间是否开启了直播 
            liveUrl: (state) => state.roomAgora.liveUrl, // 当前房间的直播链接
        }),
        ...mapGetters({
            getDemo: 'roomAgora/getDemo',
            uid_: 'user/getUserUid',
            sreenUserList: 'roomAgora/getsreenUserList'
        })
    },
    methods: {
        ...mapActions('roomAgora', ['getRoomInfo', 'downOutClass']),
        /**
         * @name: 页面之间 进行消息通信
         * @msg: 
         * @return {*}
         */
        getInitBC() {
            userMsgBc.onmessage = (res) => {
                let { type, val, name } = res.data;
                // 修改所有用户的书写状态
                if (type == 'allBoardUsing') {
                    this.isAllWrite = val
                    if (val) {
                        this.sendCommonMsg('702') // 听讲都可以写
                    } else {
                        this.sendCommonMsg('703') // 听讲都不可以写
                    }
                }

                // 获取其他页面，传递过来的参数 某个页面最小化
                if (type == 'isMini') {
                    window.electronAPI.resize('miniPage', { pageName: name, isMini: true })
                }

                // 主讲关闭了白板
                if (type == 'boardClose' && this.isOpenWhiteboard) {
                    this.isAllWrite = false
                    this.sendCommonMsg('7')
                    this.isOpenWhiteboard = false
                }

                // 主讲已经打开了白板，需要群发通知给听讲
                if (type == 'boardOpen' && !this.isOpenWhiteboard) {
                    this.isOpenWhiteboard = true;
                    this.sendCommonMsg('6');
                }
                
                // 副屏页面打开了
                if (type == 'audienceIsOpen') {
                    this.isHasSecondScreen = true;
                    this.sendUserListToAudience();
                }

                // 副屏页面关闭了
                if (type == 'audienceIsClose') {
                    this.isHasSecondScreen = false;
                }

                // 通知主进程需要关闭所有，重新进行登录
                if (type == 'reLogin') {
                    window.electronAPI.resize('reLogin')
                }

                // 听讲页面  关闭页面
                if (type == "closeWindow" && this.$route.name == 'listenSpeaker') {
                    this.userOut()
                }
            }
        },
        /**
         * @name: 将当前 房间信息发送给 副屏页面
         * @msg: 
         * @return {*}
         */        
        async sendUserListToAudience () {
            let sreenUserList = this.sreenUserList.map(s => {
                let name = s.name || s.phone
                return { uid: s.uid, isfull: s.isfull, name };
            });
            await myIndexedDB.setItem('nowUserList', sreenUserList);
            // 通知副屏页面 可以获取列表了
            userMsgBc.postMessage({ type: "userChange" });
        },
        /**
         * @name: 获取房间详情
         * @msg: 
         * @return {*}
         */
        async getRoomD() {
            let code = this.code;
            let res = await this.getRoomInfo(code);
            if (!res || this.isShowLiveWrap) return;
            // 开始推拉流操作
            await this.initAgora();
            this.startWebSocketInit();
        },
        exit(type) {
            // 下课
            if (type == 2) this.downClassConfirm()
            // 最小化
            if (type == 1) window.electronAPI.resize('mini')
            // 打开个人中心
            if (type == 3) goUsercenter()
        },
        /**
         * @name: 听讲主动退出页面
         * @msg: 
         * @return {*}
         */
        userOut() {
            this.downClassConfirm('确认退出当前课堂?', 2, true)
        },
        /**
         * @name: 是否下课?
         * @msg: 
         * @return {*}
         */
        downClassConfirm(message = '是否确定下课？', type, showTips = true) {
            let code = this.code;
            const beforeClose = (action) => {
                return new Promise(async resolve => {
                    if (action === "cancel") return resolve(true);
                    // 当前正在观看直播
                    if (type == 2 && this.isShowLiveWrap) {
                        this.downClassSuccess(res, showTips);
                        return resolve(true);
                    }
                    let res = await this.downOutClass({ code, type });
                    this.downClassSuccess(res, showTips);
                    return resolve(true);
                });
            };
            showConfirmDialog({
                closeOnPopstate: true,
                title: '温馨提示', message, confirmButtonColor: '#fe6400', beforeClose
            }).catch(() => { })
        },
        /**
         * @name: 打开副屏
         * @msg:
         * @return {*}
         */
        openAudience() {
            if (this.windoWaudience && !this.windoWaudience.closed) return;
            this.windoWaudience = openAudience(this.code);
        },

        // 设置某个用户全屏取消全屏
        isSet () {
            // 实时检测一下副屏是否 已经打开了
            this.isHasSecondScreen = this.windoWaudience && !this.windoWaudience.closed;
            this.sendUserListToAudience();
        }
    }
}
export default minix
